package com.knife.blog.web.action;

//import com.knife.blog.domain.BlogCircle;
import com.knife.blog.domain.BlogNewsDoc;
//import com.knife.blog.logic.BlogCircleDocService;
import com.knife.blog.logic.BlogCircleService;
import com.knife.blog.logic.BlogNewsDocService;
import com.knife.blog.logic.CurrentUser;
//import com.knife.blog.logic.impl.BlogCircleDocServiceImpl;
import com.knife.blog.logic.impl.BlogCircleServiceImpl;
import com.knife.blog.logic.impl.BlogCoreServiceImpl;
import com.knife.blog.logic.impl.BlogMessageServiceImpl;
import com.knife.blog.logic.impl.BlogReviewServiceImpl;
import com.knife.blog.logic.impl.PhotoServiceImpl;
import com.knife.util.CommUtil;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;
import com.knife.web.tools.IActiveUser;
import com.knife.web.tools.IPageList;
import com.knife.blog.domain.BlogInfo;
import com.knife.blog.logic.impl.BlogUserServiceImpl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class BlogNewsDocAction extends BaseCrudAction {

    private BlogNewsDocService blogDocService = BlogCoreServiceImpl.getInstance();
    private BlogCircleService circleService = BlogCircleServiceImpl.getInstance();

    public IPageList doQuery(WebForm form, int currentPage, int pageSize) {
        return blogDocService.queryBlogNewsDoc(this.getCurrentUser(form).getUserName(), currentPage, pageSize);
    }

    // 处理搜索
    public Page doManageSearch(WebForm form, Module module) {
        int currentPage = CommUtil.null2Int(form.get("page"));
        int pageSize = CommUtil.null2Int(form.get("pageSize"));
        //String dirId = CommUtil.null2String(form.get("cid"));
        if (currentPage < 1) {
            currentPage = 1;
        }
        //if (pageSize < 1)
        pageSize = 10;
        String query = CommUtil.null2String(form.get("queryTitle"));
        IPageList pageList = blogDocService.queryBlogNewsDoc(null, null, null, null, null,
                query, currentPage, pageSize);
        // System.out.println("查询到的文章数目是："+pageList.getRowCount());
        CommUtil.saveIPageList2WebForm(pageList, form);
        return module.findPage("manageList");
    }
//	 处理搜索

    public Page doSearch(WebForm form, Module module) {
        int currentPage = CommUtil.null2Int(form.get("page"));
        int pageSize = CommUtil.null2Int(form.get("pageSize"));
        String dirId = CommUtil.null2String(form.get("parentsn"));
        if ("".equals(dirId)) {
            dirId = null;
        }
        if (currentPage < 1) {
            currentPage = 1;
        }
        //if (pageSize < 1)
        pageSize = 10;
        String query = CommUtil.null2String(form.get("queryTitle"));
        IPageList pageList = blogDocService.queryBlogNewsDoc(null, null, dirId, null, null,
                query, currentPage, pageSize);
        //System.out.println("查询到的文章数目是：" + pageList.getRowCount());
        CommUtil.saveIPageList2WebForm(pageList, form);
        return module.findPage("list");
    }

    @Override
    public Page doAdd(WebForm form, Module module, IActiveUser user) {
    	boolean ret=false;
        BlogNewsDoc doc = (BlogNewsDoc) form2Obj(form);
        doc.setReadtimes(0);
        doc.setReplynum(0);
        try {
        	doc.setDisplaytime(new SimpleDateFormat("yyyy-MM-dd").parse(form.get("displaytime").toString()));
        	System.out.print("time ok");
        } catch (Exception e) {
        	doc.setDisplaytime(new Date());
        	System.out.print("time fail");
        	//e.printStackTrace();
        }
        String review = CommUtil.null2String(form.get("review"));
        if (!"".equals(review)) {
            if (review.equals("0")) {
                doc.setEnablereview(1);
                doc.setGuestreview(-1);
            } else if (review.equals("1")) {
                doc.setEnablereview(1);
                doc.setGuestreview(1);
            } else if (review.equals("-1")) {
                doc.setEnablereview(-1);
            }
        }else{
        	doc.setEnablereview(1);
        	doc.setGuestreview(-1);
        }
        try{
        	ret = blogDocService.writeBlogDoc(doc);
        }catch(Exception e){
        	ret = false;
        	e.printStackTrace();
        }
        //更新首页
        if (ret) {
            String circleSn = (String) form.get("circlesn");
            if (!circleSn.equals("") && circleSn != null) {
		BlogInfo circle = circleService.getBlogCircleByCid(circleSn);
		circle.setUpdated(Boolean.TRUE);
		circleService.updateBlogCircle(circle);
            }
             blog.setUpdated(Boolean.TRUE);
             BlogUserServiceImpl.getInstance().settingBlogInfo(blog);
            form.addResult("msg", "添加成功!");
            //System.out.println("displaytime:" + (String) form.get("displaytime"));
            return super.doQuery(form, module, user);
        } else {
            form.addResult("msg", "添加失败!");
            return module.findPage("edit");
        }
    }

    @Override
    public Page doDel(WebForm form, Module module, IActiveUser user) {
        BlogNewsDoc doc = (BlogNewsDoc) form2Obj(form);
        boolean ret = blogDocService.delBlogNewsDoc(doc.getCid());
        form.addResult("msg", ret ? "删除成功!" : "删除失败!");
        return super.doQuery(form, module, user);
    }

    public Page doManageDel(WebForm form, Module module) {
        BlogNewsDoc doc = (BlogNewsDoc) form2Obj(form);
        boolean ret = blogDocService.delBlogNewsDoc(doc.getCid());
        form.addResult("msg", ret ? "删除成功!" : "删除失败!");
        return doManageList(form, module);
    }

    @Override
    public Page doNew(WebForm form, Module module, IActiveUser user) {
        return module.findPage("edit");
    }

    public Page doManageGenerate(WebForm form, Module module) {
        BlogNewsDoc doc = (BlogNewsDoc) form2Obj(form);
        boolean ret = blogDocService.generateBlogNewsDoc(doc.getCid());
        form.addResult("msg", ret ? "生成成功!" : "生成失败!");
        return doManageList(form, module);
    }

    public Page doManageGenerateBatch(WebForm form, Module module) {
        String MulitCid = CommUtil.null2String(form.get("mulitCid"));
        String[] s = MulitCid.split(",");
        int num = blogDocService.batchGenerateBlogNewsDoc(s);
        form.addResult("msg", "成功生成了" + num + "条信息!");
        return this.doManageList(form, module);
    }

    public Page doManageGenerateAll(WebForm form, Module module) {
        int num=0;
        List docList = blogDocService.getTopBlogNewsDocBySql("",blogDocService.getBlogNewsDocNum("",""));
        for(int i=0;i<docList.size();i++){
            BlogNewsDoc doc = (BlogNewsDoc)docList.get(i);
            boolean ret = blogDocService.generateBlogNewsDoc(doc.getCid());
            if(ret){
                num++;
            }
        }
        form.addResult("msg", "成功生成了" + num + "条信息!");
        return this.doManageList(form, module);
    }

    public Page doManageEdit(WebForm form, Module module) {
        String cid = (String) form.get("cid");
        int currentPage = CommUtil.null2Int(form.get("page"));
        if (currentPage < 1) {
            currentPage = 1;
        }
        BlogNewsDoc doc = blogDocService.getBlogNewsDoc(cid);
        form.addPo(doc);
        form.addResult("page", currentPage);
        return module.findPage("manageEdit");
    }

    public Page doDelBatch(WebForm form, Module module) {
        String MulitCid = CommUtil.null2String(form.get("mulitCid"));
        String[] s = MulitCid.split(",");
        int num = blogDocService.batchDelBlogNewsDoc(s);
        form.addResult("msg", "成功删除了" + num + "条信息的状态!");
        return super.doQuery(form, module, this.getCurrentUser(form));
    }

    public Page doManageDelBatch(WebForm form, Module module) {
        String MulitCid = CommUtil.null2String(form.get("mulitCid"));
        String[] s = MulitCid.split(",");
        int num = blogDocService.batchDelBlogNewsDoc(s);
        form.addResult("msg", "成功删除了" + num + "条信息的状态!");
        return this.doManageList(form, module);
    }

    @Override
    public Page doUpdate(WebForm form, Module module, IActiveUser user) {
        BlogNewsDoc doc = (BlogNewsDoc) form2Obj(form);
        String review = CommUtil.null2String(form.get("review"));
        if (!"".equals(review)) {
            if (review.equals("0")) {
                doc.setEnablereview(1);
                doc.setGuestreview(-1);
            } else if (review.equals("1")) {
                doc.setEnablereview(1);
                doc.setGuestreview(1);
            } else if (review.equals("-1")) {
                doc.setEnablereview(-1);
            }
        }else{
            doc.setEnablereview(1);
            doc.setGuestreview(-1);
        }
        boolean ret = blogDocService.updateBlogNewsDoc(doc);
        form.addResult("msg", ret ? "修改成功!" : "修改失败!");
        if (user.getUserName().equals("admin")) {
            return this.doManageList(form, module);
        } else {
            return super.doQuery(form, module, user);
        }
    }

    public Page doShow(WebForm form, Module module) {
        String cid = CommUtil.null2String(form.get("cid"));
        BlogNewsDoc doc = blogDocService.readBlogNewsDoc(cid);
        form.addResult("obj", doc);
        blog = userService.getUserBlog(doc.getInputuser());
        form.addResult("blog", blog);
        return new Page("show" + blog.getUsername(), "/user/admin/show.htm", "template");
    }

    public Page doManageList(WebForm form, Module module) {
        if (!com.knife.blog.logic.BlogAdminConfig.getInstance().isAdmin(
                this.getCurrentUser(form).getUserName())) {
            //System.out.println(this.getCurrentUser(form).getUserName());
            return new Page("popedomError", "/manage/norights.htm", "page");
        }
        int currentPage = CommUtil.null2Int(form.get("page"));
        int pageSize = CommUtil.null2Int(form.get("pageSize"));
        if (currentPage < 1) {
            currentPage = 1;
        }
        if (pageSize < 1){
            pageSize = 15;
        }
        IPageList pageList = blogDocService.queryBlogNewsDoc(null, currentPage,
                pageSize);
        CommUtil.saveIPageList2WebForm(pageList, form);
        return module.findPage("manageList");
    }

    public Page doSetElite(WebForm form, Module module) {
        if (!com.knife.blog.logic.BlogAdminConfig.getInstance().isAdmin(
                this.getCurrentUser(form).getUserName())) {
            return new Page("popedomError", "/manage/norights.htm", "page");
        }
        String cid = CommUtil.null2String(form.get("cid"));
        BlogNewsDoc obj = blogDocService.getBlogNewsDoc(cid);
        obj.changeElite();
        blogDocService.updateBlogNewsDoc(obj);
        form.addResult("msg", "设置成功！");
        return super.doQuery(form, module, this.getCurrentUser(form));
    }

    public Page doManageSetElite(WebForm form, Module module) {
        if (!com.knife.blog.logic.BlogAdminConfig.getInstance().isAdmin(
                this.getCurrentUser(form).getUserName())) {
            return new Page("popedomError", "/manage/norights.htm", "page");
        }
        String cid = CommUtil.null2String(form.get("cid"));
        BlogNewsDoc obj = blogDocService.getBlogNewsDoc(cid);
        obj.changeElite();
        blogDocService.updateBlogNewsDoc(obj);
        form.addResult("msg", "设置成功！");
        return this.doManageList(form, module);
    }

    public Page doSetTodayHead(WebForm form, Module module) {
        if (!com.knife.blog.logic.BlogAdminConfig.getInstance().isAdmin(
                this.getCurrentUser(form).getUserName())) {
            return new Page("popedomError", "/manage/norights.htm", "page");
        }
        String cid = CommUtil.null2String(form.get("cid"));
        BlogNewsDoc obj = blogDocService.getBlogNewsDoc(cid);
        obj.changeHead();
        blogDocService.updateBlogNewsDoc(obj);
        form.addResult("msg", "设置成功！");
        return super.doQuery(form, module, this.getCurrentUser(form));
    }

    public Page doManageSetTodayHead(WebForm form, Module module) {
        if (!com.knife.blog.logic.BlogAdminConfig.getInstance().isAdmin(
                this.getCurrentUser(form).getUserName())) {
            return new Page("popedomError", "/manage/norights.htm", "page");
        }
        String cid = CommUtil.null2String(form.get("cid"));
        BlogNewsDoc obj = blogDocService.getBlogNewsDoc(cid);
        obj.changeHead();
        blogDocService.updateBlogNewsDoc(obj);
        form.addResult("msg", "设置成功！");
        return this.doManageList(form, module);
    }

    @Override
    public void doAfter(WebForm form, Module module) {
        form.addResult("dirService", BlogCoreServiceImpl.getInstance());
        form.addResult("cocService", BlogCoreServiceImpl.getInstance());
        form.addResult("currentUser", CurrentUser.getActiveUser());
        form.addResult("photoService", PhotoServiceImpl.getInstance());
        form.addResult("reviewService", BlogReviewServiceImpl.getInstance());
        form.addResult("messageService", BlogMessageServiceImpl.getInstance());
        form.addResult("circleService", BlogCircleServiceImpl.getInstance());
        if (blog != null) {
            form.addResult("blog", blog);
            form.addResult("templateName", blog.getTemplate());
        }
        super.doAfter(form, module);
    }

    public Object form2Obj(WebForm form) {
        String cid = CommUtil.null2String(form.get("cid"));
        BlogNewsDoc obj = blogDocService.getBlogNewsDoc(cid);
        if (obj == null) {
            obj = new BlogNewsDoc();
        }
        form.toPo(obj);
        return obj;
    }

    public void setBlogDocService(BlogNewsDocService blogDocService) {
        this.blogDocService = blogDocService;
    }
}
